// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace TaskbarConcepts
{
    /// <summary>
    /// Interaction logic for ThumbnailClip.xaml
    /// </summary>
    public partial class ThumbnailClip : UserControl
    {
        public ThumbnailClip()
        {
            InitializeComponent();
            this.Loaded += ThumbnailClip_Loaded;
        }

        private void ThumbnailClip_Loaded(object sender, RoutedEventArgs e)
        {
            mediaElement.MediaOpened += mediaElement_MediaOpened;
        }

        private void OnOpenMedia(object sender, RoutedEventArgs args)
        {
            CommonOpenFileDialog cfd = new CommonOpenFileDialog();
            CommonFileDialogFilter rtfFilter = new CommonFileDialogFilter("Windows media files", ".wmv");
            cfd.Filters.Add(rtfFilter);

            if (cfd.ShowDialog() == CommonFileDialogResult.OK)
            {
                mediaElement.Source = new Uri(cfd.FileName, UriKind.RelativeOrAbsolute);
            }
        }

        //The following method is part of the lab. Get the offset of the media element from the
        //main window and set the thumbnail clip to start at the offset and have the precise size
        //of the media element.
        private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            Vector offset = Utilities.GetOffset(Application.Current.MainWindow, (Visual)mediaElement);

            Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {
                    IntPtr mainWindowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
                    System.Drawing.Rectangle clipRect = new System.Drawing.Rectangle(
                        (int)offset.X, (int)offset.Y, (int)mediaElement.ActualWidth, (int)mediaElement.ActualHeight);
                    
                    TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(mainWindowHandle, clipRect);
                }));
        }
    }
}